<?php include "methods.php"; 

if ($_REQUEST){
	//lom::loop($_REQUEST);

	if ($_REQUEST['edit']){
		$edit = mysql_escape_string($_REQUEST['edit']);
		$sql = "select * from need where auto = {$edit}";
		$edit = lom::getit($sql);

	}

	if ($_REQUEST['delete']){
		$delete = mysql_escape_string($_REQUEST['delete']);
		$sql = "delete from need where auto = {$delete}";
		print lom::insert($sql);

	}

	//Validate

	$datestart = mysql_escape_string($_REQUEST['datestart']);
	$datestop = mysql_escape_string($_REQUEST['datestop']);
	$start = mysql_escape_string($_REQUEST['start']);
	$stop = mysql_escape_string($_REQUEST['stop']);
	$qty= mysql_escape_string($_REQUEST['qty']);


        $floor = floor($start);
        $r = $start -  floor($start);
        if ($r == .5){$starttime = "{$floor}:30:00 $date";}
        else{$starttime = "{$floor}:00:00 $date";}
	$start = strtotime("$starttime");

        $floor = floor($stop);
        $r = $start -  floor($stop);
        if ($r == .5){$stoptime = "{$floor}:30:00 $date";}
        else{$stoptime = "{$floor}:00:00 $date";}
	$stop = strtotime("$stoptime");





		$sql  = "insert into need set ";
		$sql .= "start = $start, ";
		$sql .= "stop = $stop, ";
		$sql .= "qty = $qty";

//print "$sql\n";
		lom::insert($sql);


}


?>
<html>
<head>
</head>
<body>
<h1>Enter Weekly Schedule of Needs</h1>


<h2>Needs</h2>

<form action='need.php' method='POST'>

<div style="width: 100%; overflow: hidden;">
<div style="float: left; width: 250px; border: 1px solid red;">

<?php

if (count($edit) > 0){print "<h1>Editing Need</h1>\n";
print "<input type='hidden' name='edit' value='{$_REQUEST['edit']}' />\n";}



$randdate=date("m/d/y");

print "<br><br>";
print "<table><tr><td>Date:</td><td>";
print "<input type='text' name='datestart' value='{$randdate}' size=7></td></tr>\n";
print "<tr><td>Start Time of Slot";
print "</td><td>";
print "<select name='start'>";

//calculate $select
if ($_REQUEST['start']){$select = $_REQUEST['start'];}
elseif ($edit[0]['start']){$select = $edit[0]['start'];}
else if ($start){$select = $start;}
else {$select =7;}



for ($i=0; $i <= 24; $i+=0.5){
	$floor = floor($i);
	$r = $i -  floor($i);
        if ($r == .5){$time = "{$floor}:30:00";}
        else{$time = "{$floor}:00:00";}
        $display = date("g:i a", strtotime($time)) ;
	if ($i == $select){$checked = "selected"; }
	else{$checked = null;}
	print "<option value='$i' $checked>$display</option>\n";
}
print "</select>\n";
print "</td></tr>\n";
print "<tr><td>Stop Date:</td><td>";
print "<input type='text' name='datestop' value='{$randdate}' size=7></td></tr>\n";
print "<tr><td>";
print "End Time of Slot";
print "</td><td>";
print "<select name='stop'>";

//calculate $select
if ($_REQUEST['stop']){$select = $_REQUEST['stop'];}
elseif ($edit[0]['stop']){$select = $edit[0]['stop'];}
else if ($stop){$select = $stop;}
else {$select =7  + MAX_SHIFT_LEN;}

for ($i=0; $i <= 24; $i+=0.5){

	$floor = floor($i);
	$r = $i -  floor($i);
	if ($r == .5){$time = "{$floor}:30:00";}
	else{$time = "{$floor}:00:00";}
	$display = date("g:i a", strtotime($time)) ;
        if ($i == $select){$checked = "selected"; }
        else{$checked = null;}
	print "<option value='$i' $checked>$display</option>\n";
}
print "</select>\n";
print "</td></tr>\n";

print "<tr><td>";
print "Number of Workers";
print "</td><td>";
print "<select name='qty'>";
for ($i=0; $i < 6; $i++){
        if ($i == 1){$checked = "selected"; }
        else{$checked = null;}
        print "<option value='$i' $checked>$i</option>\n";

}
print "</select>\n";
print "</td></tr>\n";

print "</table>";

print "<br><br>";
print "<input type='submit' name='submit' value='submit' />\n";
print "<input type='reset' value='reset' />\n";
?>
</form>
</div>
<div style="border: 1px solid green; overflow:hidden;">

<?php

$sql  = "select * ";
$sql .= "from need order by start asc, ";
$sql .= "stop asc ";
$results = lom::getit($sql);


if (count($results) == 0){
	print "<h1>No Needed shifts have yet been entered</h1>\n";
}else{

$return = "<table border=1;>\n";

$return .= "<tr><th>Start</th><th>Stop</th><th>Workers</th><th></th><th></th></tr>\n";



foreach ($results as $r) {
	$days = null;

$start = date("M d, y H:i:s", $r['start']);
$stop = date("M d, y H:i:s", $r['stop']);
$auto = $r['auto'];
$qty = $r['qty'];



	$return .= "<tr>";
        $return .= "<td>$start</td>";
        $return .= "<td>{$stop}</td>";
        $return .= "<td>$qty</td>";

	$return .= "<td><a href='need.php?edit={$auto}'>Edit</a></td>";
	$return .= "<td><a href='need.php?delete={$auto}'>Delete</a></td>";

        $return .= "</tr>\n";


}//foreach
	$return .= "</table>\n\n";
	print $return;

}//if (count($results) == 0)

?>


<br>
<br>
<br>
<br>


</div>
</div>




</body>
</html>
